<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <%layout("/WEB-INF/view/common/inc/admin_head.html",{title:'', styles:'bootstrap,font-awesome,animate,style,bootstrap-table,layui-date,auto-complete,dtvalidate,layui,chosen'}){%>
    <style type="text/css">
        body{padding-top:10px;
            padding-left: 10px;}
        *{margin:0;padding:0;list-style-type:none;}
        a,img{border:0;}
        .demo h2{font-size:16px;color:#3366cc;height:30px;}
        .demo li{float:left;}
    </style>
    <%}%>
</head>

<body>
<div class="row">
    <div class="col-sm-12">

        <!-- START 右侧数据 -->
        <div class="ibox float-e-margins" style="margin-bottom: 0px;">
            <div id="toolbar" class="btn-group" style="margin-bottom:5px;">
                <button  class="report-btn layui-btn  layui-btn-sm"  style="margin-top: -6px;margin-left: -4px">
                    <i class="fa fa-check-square-o" aria-hidden="true"></i> 生成质检报告
                </button>
                <button  class="export-btn layui-btn  layui-btn-sm" style="margin-top: -6px;margin-left: -2px">
                    <i class="fa fa-download" aria-hidden="true"></i> 导出当前数据
                </button>
            </div>
            <!-- START 操作按钮 -->
            <!-- END 操作按钮 -->

            <!-- START 数据表格 -->
            <table id="relatedTable"
                   data-toolbar="#toolbar"
                   data-side-pagination="server"
                   data-pagination="true"
                   data-resizable="true"
                   data-url="/sea/supply/quality/produce/findByCnd.do"
                   data-page-size="15"
                   class="table table-striped table-hover"
            >
            </table>
            <!-- END 数据表格 -->
        </div>
    </div>
</div>

<!-- START 内容部分 -->
<div class="container" style="display: none;overflow-x: hidden;max-width: 600px" id="layer_window">
    <form class="form-horizontal" id="info-form" enctype="multipart/form-data" autocomplete="off" >
        <input type="hidden" name="id" id="id">
        <br />
        <div class="form-group" style="max-width: 600px;min-width: 580px">
            <label class="control-label col-sm-4">选择文件:</label>
            <div class="col-sm-6">
                <a class="club-import-btn ">
                    <input type="file" id="importSession" name="importSession">
                </a>
                <input type="hidden" name="qualityFile" id="qualityFile">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label col-sm-3 col-sm-offset-1">备注:</label>
            <div class="col-sm-6">
                <textarea id="qualityRemark" name="qualityRemark" class="form-control"  rows="3"></textarea>
            </div>

        </div>
    </form>
    <div class="form-group">
        <div class="col-sm-offset-4 col-sm-10">
            <button type="submit" class="save-btn btn btn-primary">
                <i class="fa fa-save"></i> 保存
            </button>
        </div>
    </div>
</div>


<div id="layer_supplier_window" style="display: none;overflow-x: hidden;overflow-y: hidden">
    <!-- START 内容部分 -->
    <div class="container" >
        <form class="form-horizontal" id="info-supplier-form" method="post" autocomplete="off" style="margin: auto">
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-2" style="max-width: 100px">质检结果</label>
                <div class="col-sm-8" style="max-width: 300px;">
                    <select  id="qualityResult" name="qualityResult" class="form-control" >
                        <option value="">请选择</option>
                        <option value="0">质检通过</option>
                        <option value="1">质检未通过</option>
                        <option value="2">二次质检</option>
                    </select>
                </div>
            </div>
            <div class="form-group" style="margin-top: 20px">
                <label class="control-label col-sm-7" style="max-width: 100px"></label>
                <button type="button" class="save-produce-btn btn btn-primary">
                    <i class="fa fa-save"></i> 保存
                </button>
            </div>
        </form>
    </div>
    <!-- END 内容部分 -->
</div>
<%layout("/WEB-INF/view/common/inc/admin_js.html",{modules:'jquery,bootstrap,bootstrap-table,dtvalidate,dtvalidate-lang-zh_cn,bootstrap-table-zh-CN,laydate,layer,chosen,chosen-demo'}){}%>
<script>
    var $relatedTable = $('#relatedTable');
    var cur_table='';
    $(function(){
        //START 表格配置
        $relatedTable.bootstrapTable({
            detailView: true,
            columns: [

                {
                    title: '采购单号',
                    field: 'produceNo',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '供应商',
                    field: 'produceSupplierName',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '质检情况',
                    field: 'con',
                    align: 'center',
                    valign: 'middle'
                },{
                    title: '质检合格率',
                    field: 'per',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        return v + '%';
                    }
                },{
                    title: '下单时间',
                    field: 'createTime',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        if(null != v && v.length>10) {
                            return v.substr(0, 10);
                        }else{
                            return v;
                        }
                    }
                },{
                    title: '质检报告',
                    field: 'qualityFile',
                    align: 'center',
                    valign: 'middle',
                    formatter:function (v,r) {
                        if(null!=v && v!=''){
                            var result='<button  class="download-btn layui-btn  layui-btn-sm"  style="margin-top: -6px;margin-left: -4px">';
                            result+='<i class="fa fa-download" aria-hidden="true"></i> <a href="'+v+'" download style="color: white">下载附件</a>';
                            result+='</button>';
                            return result;
                        }else{
                            return '';
                        }
                    }
                },{
                    title: '质检备注',
                    field: 'qualityRemark',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    title: '操作',
                    field: 'operate',
                    align: 'center',
                    valign: 'middle',
                    formatter: optFormatter
                }
            ],
            onLoadSuccess : function(){

                $(".edit-sku-btn").on('click',function () {
                    var id = $(this).attr('data-val');
                    $("#id").val(id);
                    layer.open({
                        type:1,
                        title:'导入明细',
                        area:['600px','400px'],
                        skin:'layer-window',
                        shadeClose:false,
                        resize: false,
                        scrollbar: false,
                        maxmin: false,//出现最大化按钮
                        content:$('#layer_window')
                    });
                });

            },onExpandRow: function (index, row, $detail) {
                InitSubTable(index, row, $detail);
            }
        });

        //初始化子表格(无线循环)
        function InitSubTable (index, row, $detail) {
            var produceId = row.produceId;
            cur_table = $detail.html('<table id="disTable"></table>').find('table');
            $(cur_table).bootstrapTable({
                url: '/sea/supply/quality/produce/detail/findByProduceId',
                method: 'get',
                clickToSelect: true,
                queryParams: { produceId: produceId},
                ajaxOptions: { id: produceId },
                clickToSelect: true,
                //height: 500,
                detailView: false,//父子表
                uniqueId: "id",
                pageSize: 10,
                pageList: [10, 25],
                columns: [
                    {
                        field: 'state',
                        checkbox: true,
                        align: 'center',
                        valign: 'middle'
                    },
                    {
                        field: 'produceType',
                        title: '类型'
                    },{
                        field: 'produceName',
                        title: '属性'
                    },{
                        field: 'qualityStatus',
                        title: '合格',
                        align: 'center',
                        valign: 'middle',
                        formatter:function (v,r) {
                            if(v=='0'){
                                return '√';
                            }else{
                                return '';
                            }
                        }
                    },{
                        field: 'qualityStatus',
                        title: '不合格',
                        align: 'center',
                        valign: 'middle',
                        formatter:function (v,r) {
                            if(v=='1'){
                                return '√';
                            }else{
                                return '';
                            }
                        }
                    },{
                        field: 'qualityStatus',
                        title: '二次质检',
                        align: 'center',
                        valign: 'middle',
                        formatter:function (v,r) {
                            if(v=='2'){
                                return '√';
                            }else{
                                return '';
                            }
                        }
                    }],
                onLoadSuccess : function(){

                }
            });
        };
    });


    $('.report-btn').on('click',function () {

        var selDatas = cur_table.bootstrapTable('getSelections');
        if( selDatas.length == 0 ){
            layer.msg('请选择要生成的数据',{icon: 0,shade: [0.8, '#393D49'],time: 1500});
        }else{
            layer.open({
                type:1,
                title:'生成质检单',
                area:['550px','370px'],
                skin:'layui-layer-lan',//layui-layer-molv ;layui-layer-lan;layui-layer-rim;layer-window
                shadeClose:false,
                resize: false,
                scrollbar: false,
                maxmin: false,//出现最大化按钮
                content:$('#layer_supplier_window')
            })
        }
    });


    // 操作菜单
    function optFormatter(value,row){
        var content = '';
        content += '&nbsp;<button class="edit-sku-btn btn btn-xs btn-success" data-val="'+row.id+'" title="导入质检报告">导入质检报告</button>';
        return content;
    }


    // 条件查询
    $('.search-btn').on('click',function () {
        $relatedTable.bootstrapTable('refresh', { url:'${ctxPath}/sea/supply/quality/findByQuality.do?'+$('#search-form').serialize() });
    });

    $("#importSession").on("change",function(){
        var formFile = new FormData($('#info-form')[0]);
        var index=layer.load(1, {
            shade: [0.1,'#fff'],
            zIndex : 1111//0.1透明度的白色背景
        });
        $.ajax({
            url: "/upload/uploadFile",
            data: formFile,
            type: "post",
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            success: function (data) {
                if(data.ok){
                    parent.layer.msg("上传成功！",{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    $("#qualityFile").val(data.data);
                    layer.close(index);
                }else{
                    parent.layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
                    layer.close(index);
                }
            },
        });
    });


    //提交表单
    $('.save-btn').on('click',function(){
        var qualityDate=$("#qualityDate").val();
        if(qualityDate==''){
            parent.layer.msg('请先选择质检日期！',{icon: 0,time:1500,shade: [0.8, '#393D49']});
            return;
        }
        //数据提交
        $.post('/sea/supply/quality/produce/usave.do',$('#info-form').serialize(),function(data){
            if( data.ok ){
                parent.layer.msg(data.msg,{icon:1,time:1500,shade: [0.8, '#393D49']}, function(){
                    $relatedTable.bootstrapTable('refresh');
                    layer.closeAll(); //关闭弹窗
                });
            }else{
                parent.layer.msg(data.msg,{icon: 0,time:1500,shade: [0.8, '#393D49']});
            }
        });
    });

    $(".save-produce-btn").on('click',function () {
        var qualityResult=$("#qualityResult").val();
        if(qualityResult==''){
            parent.layer.msg('请先选择质检状态！',{icon: 0,time:1500,shade: [0.8, '#393D49']});
            return;
        }
        var selDatas = cur_table.bootstrapTable('getSelections');
        if( selDatas.length == 0 ){
            layer.msg('请选择要生成的数据',{icon: 0,shade: [0.8, '#393D49'],time: 1500});
        }else{
            var ids = [];
            var flag=true;
            $.each(selDatas,function(i,d){
                ids.push(d.id);
            });
            ids = ids.join(',');
            if(flag==true) {
                var inde= layer.load(2, { //icon支持传入0-2
                    shade: [0.1, '#fff'], //0.5透明度的灰色背景
                    content: '正在生成验货报告,请稍后...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'margin-left': '-50px',
                            'width': '300px',
                            'font-color': 'red',
                            'font-size': '20px'
                        });
                    }
                });
                $.ajax({
                    url: "/sea/supply/quality/produce/detail/updateQualityStatus",
                    data: {
                        qualityStatus:$("#qualityResult").val(),
                        ids:ids
                    },
                    type: "post",
                    success: function (data) {
                        if(data.ok){
                            console.log(data);
                            layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                                    layer.closeAll();
                                    cur_table.bootstrapTable('refresh');
                                }});
                        }else{
                            layer.msg(data.msg,{icon: 0,btn: ['确定'],time:0,shade: [0.8, '#393D49'],yes: function(index, layero){ // 默认的是 按钮一
                                    layer.closeAll();
                                }});
                        }
                    },
                });
            }
        }
    })

</script>
</body>
</html>
